package rgbos.forum.domain;

import java.util.Calendar;
import java.util.Vector;

import rgbos.forum.persistance.Persistance;

public class ForumCtrlUser  {
	private Persistance pres_;
	private final int OFFLINE = 0;
	private final int ONLINE  = 1;
	protected final int REGULER = 0;
	protected final int MODERATOR = 1;
	protected final int ADMIN = 2;
	public ForumCtrlUser(Persistance pers) {
		pres_ = pers;
	}
	public synchronized boolean logIn(String userName, String pass) throws Exception {
		boolean ans =  pres_.logIn(userName, pass);
		if(ans){
			this.pres_.incHour(Calendar.getInstance().getTime().getHours());
			this.pres_.setOnlineBit(userName, ONLINE);
		}
		return ans;
	}
	public synchronized boolean registerNewUser(User newUser)  {
		return pres_.registerNewUser(newUser);
	}
	public synchronized int checkPrems(String userName){
		if(checkAdmin(userName)){
			return ADMIN;
		}
		if(checkModerator(userName)){
			return MODERATOR;
		}else {
			return REGULER;
		}

	}
//
	public synchronized boolean checkAdmin(String userName) {
		return pres_.checkAdmin(userName);
	}
	public synchronized boolean checkModerator(String userName) {
		return pres_.checkModerator(userName);
	}
	public synchronized boolean setModerator(String userName){
		return pres_.setModerator(userName);
	}
	public synchronized Vector<User> getOnlineUsers(){
		return pres_.getOnlineUsers();
	}
	public synchronized void logOut(String username) {
		this.pres_.setOnlineBit(username, OFFLINE);

	}
	public User getUserByName(String username) {
		return pres_.getUserByName(username);
	}
	public boolean modifyUser(User user) {
		return pres_.modifyUserDetails(user);
	}
	public String getMailByName(String userName) {
		return pres_.getEmailByName(userName);
	}
	
	public void closeConn() {
		this.pres_.closeConnection();
	}
	public Vector<User> getAllUsers() {
		return this.pres_.getUsers();
	}
	public Vector<String> getAllUsersName() {
		return this.pres_.getUsersName();
	}
	public boolean deleteUser(String name) {
		return this.pres_.deleteUser(name);
	}
	public int[] getHourStats() {
		return this.pres_.getHourStats();
	}
	public boolean setOffline(String username) {
		return this.pres_.setOnlineBit(username, OFFLINE);
	}
	public boolean setOnline(String username) {
		return this.pres_.setOnlineBit(username, ONLINE);
	}
}
